package 力扣.中等;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

/*
* 输入: intervals = [[1,3],[2,6],[8,10],[15,18]]
输出: [[1,6],[8,10],[15,18]]
解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
示例 2:

输入: intervals = [[1,4],[4,5]]
输出: [[1,5]]
解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
* 思路：     区间第一个元素升序排序 维护一个区间 直到下一个区间不能使其扩大
*/
public class 合并区间 {
    public static void main(String[] args) {
        ArrayList<int[]> arrayList = new ArrayList<>();
        int[][] met = {{0,4},{1,4}};
//[[2,3],[2,2],[3,3],[1,3],[5,7],[2,2],[4,6]]
        Arrays.sort(met, Comparator.comparingInt(o -> o[0]));
        for (int i = 0; i < met.length; i++) {
            arrayList.add(met[i]);
        }

        int np=0;
        for (int j = 1; j < arrayList.size(); j++) {

            for(int n=0;n<=np;n++){

                int a[] = arrayList.get(n);
                int[] b = arrayList.get(j);
                if(a==b){
                    break;}
                else {
                    int max = Math.max(a[0], b[0]);
                    int min = Math.min(a[1], b[1]);
                    if (max <= min) {
                        int[] map = {Math.min(a[0], b[0]), Math.max(a[1], b[1])};
                        arrayList.set(n, map);
                        arrayList.remove(j);
                        j--;
                        break;
                    }

                }

                if(n==np){
                    np++;
                }
            }



        }

        int[][] po =arrayList.toArray(new int[0][]);
        for(int i=0;i<po.length;i++){
            System.out.println(Arrays.toString(po[i]));
        }


    }
    class Solution {
        public int[][] merge(int[][] intervals) {
            ArrayList<int[]> arrayList = new ArrayList<>();
            Arrays.sort(intervals, Comparator.comparingInt(o -> o[0]));
            for (int i = 0; i < intervals.length; i++) {
                arrayList.add(intervals[i]);
            }

            if (intervals == null || intervals.length == 0)
                return arrayList.toArray(new int[0][]);

            int np=0;
            for (int j = 1; j < arrayList.size(); j++) {
                for(int n=0;n<=np;n++){

                    int a[] = arrayList.get(n);
                    int[] b = arrayList.get(j);
                    if(a==b){
                        break;}
                    else {
                        int max = Math.max(a[0], b[0]);
                        int min = Math.min(a[1], b[1]);
                        if (max <= min) {
                            int[] map = {Math.min(a[0], b[0]), Math.max(a[1], b[1])};
                            arrayList.set(n, map);
                            arrayList.remove(j);
                            j--;
                            break;
                        }

                    }

                    if(n==np){
                        np++;
                    }
                }
            }
            int[][] po =arrayList.toArray(new int[0][]);
            return po;

        }

    }

}
